//
//  XSShootingToolBar.m
//  AudioVideo
//
//  Created by xueshan1 on 2019/8/23.
//  Copyright © 2019 xueshan1. All rights reserved.
//

#import "XSShootingToolBar.h"
#import "XSShootingButton.h"

//static CGFloat ShotBtnWidth = 70;

@interface XSShootingToolBar ()

@property (nonatomic , strong) XSShootingButton * shootingButton;
@property (nonatomic , strong) UIButton * colseButton;
@property (nonatomic , strong) UIButton * leftButton;
@property (nonatomic , strong) UIButton * rightButton;
@property (nonatomic , strong) UIButton * editorButton;

@end

@implementation XSShootingToolBar

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self setupUI];
    }
    return self;
}

- (void)setupUI{
    self.shootingButton.frame = CGRectMake(0, 0, 70, 70);
    self.shootingButton.center = CGPointMake(self.width/2, self.height/2);
    [self.shootingButton setupLayer];
    
    self.colseButton.frame = CGRectMake(self.shootingButton.left - 100, 0, 40, 40);
    self.colseButton.centerY = self.height/2;

    self.leftButton.frame  = self.shootingButton.frame;
    self.rightButton.frame = self.shootingButton.frame;
    self.editorButton.frame = self.shootingButton.frame;
    
}

- (void)layoutSubviews{
    [super layoutSubviews];

}

/**
 小中心录制，拍照按钮
 */
-(XSShootingButton*)shootingButton
{
    if (!_shootingButton) {
        _shootingButton =[XSShootingButton new];
        _shootingButton.longPressDelegate = self;
        [self addSubview:_shootingButton];
    }
    return _shootingButton;
}
-(UIButton*)colseButton
{
    if (!_colseButton) {
        _colseButton = [[UIButton alloc]init];
        [_colseButton setImage:[UIImage imageNamed:@"icon_cancel"] forState:normal];
        _colseButton.tag = 3;
        [_colseButton addTarget:self action:@selector(closeClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_colseButton];
    }
    return _colseButton;
}
-(UIButton*)leftButton
{
    if (!_leftButton) {
        _leftButton = [[UIButton alloc]init];
        _leftButton.hidden = YES;
        _leftButton.tag = 1;
        _leftButton.backgroundColor = [UIColor whiteColor];
        [_leftButton setImage:[UIImage imageNamed:@"btn_return"] forState:normal];
        _leftButton.layer.cornerRadius=35;
        _leftButton.layer.masksToBounds = YES;
        [_leftButton addTarget:self action:@selector(leftButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_leftButton];
    }
    return _leftButton;
}
-(UIButton*)rightButton
{
    if (!_rightButton) {
        _rightButton = [[UIButton alloc]init];
        _rightButton.hidden = YES;
        _rightButton.layer.cornerRadius=35;
        _rightButton.tag = 4;
        _rightButton.layer.masksToBounds = YES;
        [_rightButton setImage:[UIImage imageNamed:@"btn_sure"] forState:normal];
        [_rightButton addTarget:self action:@selector(rightButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_rightButton];
    }
    return _rightButton;
}
-(UIButton*)editorButton
{
    if (!_editorButton) {
        _editorButton = [[UIButton alloc]init];
        _editorButton.hidden = YES;
        [_editorButton setBackgroundColor:[UIColor lightTextColor]];
        _editorButton.layer.cornerRadius=35;
        _editorButton.tag = 4;
        _editorButton.layer.masksToBounds = YES;
        [_editorButton addTarget:self action:@selector(editorButtonClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_editorButton];
    }
    return _editorButton;
}

#pragma actionClick 事件逻辑
//关闭按钮
-(void)closeClick:(UIButton*)button{
    if (self.toolBarDelegate && [self.toolBarDelegate respondsToSelector:@selector(shootingToolBarClosed:)]) {
        [self.toolBarDelegate shootingToolBarClosed:self];
    }
    
}
//左侧取消按钮
-(void)leftButtonClick:(UIButton*)button{
    [self buttonAnimation:NO];
    if (self.toolBarDelegate && [self.toolBarDelegate respondsToSelector:@selector(shootingToolBarEditCancled:)]) {
        [self.toolBarDelegate shootingToolBarEditCancled:self];
    }
    
}
//右侧确定按钮
-(void)rightButtonClick:(UIButton*)button{
    if (self.toolBarDelegate && [self.toolBarDelegate respondsToSelector:@selector(shootingToolBarEditSure:)]) {
        [self.toolBarDelegate shootingToolBarEditSure:self];
    }
}
//编辑按钮
-(void)editorButtonClick:(UIButton*)button{
    if (self.toolBarDelegate && [self.toolBarDelegate respondsToSelector:@selector(shootingToolBarEditClicked:)]) {
        [self.toolBarDelegate shootingToolBarEditClicked:self];
    }
    
}
-(void)buttonAnimation:(BOOL)open{
    _colseButton.hidden = open;
    _shootingButton.hidden =open;
    
    _editorButton.hidden = !open;
    _leftButton.hidden = !open;
    _rightButton.hidden = !open;
    
    if (open) {
        WeakSelf
        [UIView animateWithDuration:0.4 animations:^{
            weakSelf.leftButton.transform = CGAffineTransformTranslate(weakSelf.leftButton.transform, -100, 0);
            weakSelf.rightButton.transform = CGAffineTransformTranslate(weakSelf.rightButton.transform, 100, 0);
        }];
        return;
    }
    //隐藏
    _leftButton.transform = CGAffineTransformIdentity;
    _rightButton.transform = CGAffineTransformIdentity;
    
}

#pragma mark - delegate
-(void)shootingButton:(XSShootingButton *)btn longPressStart:(CGFloat)progress{
    if (self.toolBarDelegate && [self.toolBarDelegate respondsToSelector:@selector(shootingToolBarStartRecording:)]) {
        [self.toolBarDelegate shootingToolBarStartRecording:self];
    }
}
-(void)shootingButton:(XSShootingButton *)btn longPressEnd:(CGFloat)progress{
    [self buttonAnimation:YES];
    if (self.toolBarDelegate && [self.toolBarDelegate respondsToSelector:@selector(shootingToolBar:endRecordProgress:)]) {
        [self.toolBarDelegate shootingToolBar:self endRecordProgress:progress];
    }

}
-(void)shootingButtonSingleTap:(XSShootingButton *)btn{
    [self buttonAnimation:YES];
    if (self.toolBarDelegate && [self.toolBarDelegate respondsToSelector:@selector(singleTapShootBtn:)]) {
        [self.toolBarDelegate singleTapShootBtn:self];
    }
    
}



@end
